package work.SlidingWindow;

/**
 * @author WDH
 * @description
 * @date 2024年12月04日 22:07
 */

public class Problem209 {

    public int minSubArrayLen(int s, int[] nums) {

        int i = 0;
        int sum = 0;
        int ans = Integer.MAX_VALUE;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            while (sum >= s) {
                ans = Math.min(ans, j - i + 1);
                sum -= nums[i];
                i++;
            }

        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }



    public static void main(String[] args) {
        Problem209 problem = new Problem209();
        int s = 4;
        int[] nums = {1,4, 4};


        int result2 = problem.minSubArrayLen(s, nums);


        System.out.println("minSubArrayLen2 result: " + result2); // 输出: 1
    }

}
